package com.intellagric.service.iftmonitor.Impl.ift_check_item;

import com.github.pagehelper.PageHelper;
import com.intellagric.common.pojo.LayuiDataGridResult;
import com.intellagric.common.pojo.ResponseMessage;
import com.intellagric.mapper.IftCheckItemMapper;
import com.intellagric.pojo.IftCheckItem;
import com.intellagric.pojo.IftCheckItemExample;
import com.intellagric.service.iftmonitor.ift_check_item.CheckItemService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @Author: zhy
 * @email: 1269679946@qq.com
 * @Date: 2018/10/23
 * @DESC： 检测项目管理服务实现
 */
@Service
public class CheckItemServiceImpl implements CheckItemService {

    @Autowired
    private IftCheckItemMapper checkItemMapper;

    /**
     * 添加检测项目
     * @param checkItem
     * @return ResponseMessage
     */
    @Override
    public ResponseMessage addCheckItem(IftCheckItem checkItem) {
        if (checkItemMapper.insertSelective(checkItem) == 1) {
            return ResponseMessage.success();
        } else {
            return ResponseMessage.fail();
        }
    }

    /**
     * 删除检测项目
     * @param id
     * @return ResponseMessage
     */
    @Override
    public ResponseMessage deleteCheckItem(String id) {
        if (checkItemMapper.deleteByPrimaryKey(id) == 1) {
            return ResponseMessage.success();
        } else {
            return ResponseMessage.fail();
        }
    }

    /**
     * 修改检测项目信息
     * @param checkItem
     * @return ResponseMessage
     */
    @Override
    public ResponseMessage editCheckItem(IftCheckItem checkItem) {
        if (checkItemMapper.updateByPrimaryKeySelective(checkItem) == 1) {
            return ResponseMessage.success();
        } else{
            return ResponseMessage.fail();
        }
    }

    /**
     * 查询检测项目信息
     * @param id
     * @return IftCheckItem
     */
    @Override
    public IftCheckItem getCheckItemById(String id) {
        return checkItemMapper.selectByPrimaryKey(id);
    }

    /**
     * 查询检测项目列表
     * @return LayuiDataGridResult
     */
    @Override
    public LayuiDataGridResult getCheckItemList() {
        IftCheckItemExample checkItemExample = new IftCheckItemExample();
        checkItemExample.createCriteria().andIdIsNotNull();
        List<IftCheckItem> checkItemList = checkItemMapper.selectByExample(checkItemExample);
        return LayuiDataGridResult.success().add(checkItemList,checkItemList.size());
    }

    /**
     * 分页查询检测项目列表
     * @param page
     * @param rows
     * @return LayuiDataGridResult
     */
    @Override
    public LayuiDataGridResult getCheckItemPage(int page, int rows) {
        PageHelper.startPage(page,rows);
        IftCheckItemExample checkItemExample = new IftCheckItemExample();
        checkItemExample.createCriteria().andIdIsNotNull();
        List<IftCheckItem> checkItemList = checkItemMapper.selectByExample(checkItemExample);
        return LayuiDataGridResult.success().add(checkItemList,this.getCheckItemList().getCount());
    }
}
